package com.ukao.steward.ui.valetRunners.storage;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ukao.steward.R;
import com.ukao.steward.adapter.SelectStoreAdapter;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.StoreBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.T;
import com.ukao.steward.widget.MyLRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class SelectStoreFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.orderNo)
    TextView orderNo;
    @BindView(R.id.lrecycler_view)
    MyLRecyclerView lrecyclerView;
    Unbinder unbinder;
    @BindView(R.id.storge_go_btn)
    Button storgeGoBtn;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private SelectStoreAdapter mSelectStoreAdapter;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private boolean isloadFinish;
    private View mEmptyView;
    private String mOrderNo;


    public static SelectStoreFragment newInstance(String orderNo, String param2) {
        SelectStoreFragment fragment = new SelectStoreFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, orderNo);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mOrderNo= getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_store_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        mEmptyView= view.findViewById(R.id.empty_view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    @Override
    protected void initView() {
        title.setText("选择门店");
        orderNo.setText("单号："+CheckUtils.isEmptyString(mOrderNo));
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
        GridItemDecoration divider = new GridItemDecoration.Builder(_mActivity)
                .setHorizontal(R.dimen.dp_15)
                .setVertical(R.dimen.dp_15)
                .setColorResource(R.color.transparent)
                .build();
        initGridRecyclerView(lrecyclerView, 2);
        List<StoreBean> mStoreData = new ArrayList<>();
        mSelectStoreAdapter = new SelectStoreAdapter(_mActivity, mStoreData);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mSelectStoreAdapter);
        lrecyclerView.setAdapter(mLRecyclerViewAdapter);
        lrecyclerView.addItemDecoration(divider);

    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        setStatusBarTintColor(R.color.colorPrimary);
        if (!isloadFinish)
            lrecyclerView.forceToRefresh();

    }

    @Override
    protected void initListener() {
        super.initListener();
        mLRecyclerViewAdapter.setOnItemClickListener((view, pos) -> {
                    StoreBean mItem = mSelectStoreAdapter.getItem(pos);
                    if (mItem != null) {
                        if (mItem.isCheck()) return;
                        mItem.setCheck(true);
                        for (int i = 0; i < mSelectStoreAdapter.getDataList().size(); i++) {
                            StoreBean storeBean = mSelectStoreAdapter.getDataList().get(i);
                            if (pos != i) {
                                storeBean.setCheck(false);
                            }
                        }
                        mSelectStoreAdapter.notifyDataSetChanged();
                    }
        });
        lrecyclerView.setOnRefreshListener(() -> {

            storelist();
        });
    }

    /**
     * 店铺列表
     */
    public void storelist() {

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores().storelist(Constant.createParameter(mHashMap)),
                new ApiCallback<StoreBean>() {


                    @Override
                    public void onSuccess(StoreBean model) {

                        if (model.getHttpCode() == 200) {
                            isloadFinish = true;
                            mEmptyView.setVisibility(CheckUtils.isEmpty(model.getData())?View.VISIBLE:View.GONE);
                            mSelectStoreAdapter.setDataList(model.getData());

                        } else {
                            T.show(model.getMsg());
                        }

                    }

                    @Override
                    public void onFinish() {
                        lrecyclerView.refreshFinish(Constant.SEND_FACTORY_PAGE_SIZE);

                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.back_btn,R.id.storge_go_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case  R.id.storge_go_btn:
                if (!isloadFinish||CheckUtils.isEmpty(mSelectStoreAdapter.getDataList())){
                    T.show("未获取到门店");
                    return;
                }
                String  storeId="";
                for (StoreBean storeBean : mSelectStoreAdapter.getDataList()) {
                    if (   storeBean.isCheck()){
                        storeId=storeBean.getId();
                    }
                }
                if (CheckUtils.isEmpty(storeId)){
                     T.show("请选择门店");
                    return;
                }
                start(StorageListFragment
                        .createBuilder()
                        .orderNo(mOrderNo)
                        .storeId(storeId).build());

                break;

        }
    }
}
